package com.shark.demo;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @ClassName CyclicBarrierDemo
 * @Description TODO
 * @Author 东北彭于晏
 * @Date 2024/9/29  15:33
 * @Version 1.0
 **/
public class CyclicBarrierDemo {
    public static Integer number = 7;

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(number, () -> {
            System.out.println("集齐七颗龙珠就可以召唤神龙");
        });

        for (int i = 0; i < 7; i++) {
            int finalI = i;
            new Thread(() -> {
                try {
                    System.out.println("🐲" + finalI + "被收集");
                    cyclicBarrier.await();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }

    }
}
